﻿using AutoMapper;
using Modules.Attachment;
using Modules.Constructor;
using Modules.Corp;
using Modules.System.Area;
using PmSoft;
using PmSoft.ApplicationContext;

namespace ManagementApi.Models.Corp;

public class CorpInfoDto : CorpBaseInfoDto
{
    /// <summary>
    /// 办公地址 
    ///</summary>
    public string OfficeAddr { get; set; }
    /// <summary>
    /// 法人姓名 
    ///</summary>
    public string Legalman { get; set; }
    /// <summary>
    /// 法人身份证号码 
    ///</summary>
    public string LegalCardno { get; set; }
    /// <summary>
    /// 法人手机号码 
    ///</summary>
    public string LegalMobile { get; set; }
    /// <summary>
    /// 法人职务 
    ///</summary>
    public string LegalDuty { get; set; }
    /// <summary>
    /// 法人职称 
    ///</summary>
    public string LegalTitle { get; set; }
    /// <summary>
    /// 经济类型 
    ///</summary>
    public CorpEconomicType EconomicType { get; set; }
    /// <summary>
    /// 经济类型名称 
    ///</summary>
    public string EconomicTypeName { get; set; }
    /// <summary>
    /// 邮编 
    ///</summary>
    public string ZipCode { get; set; }
    /// <summary>
    /// 注册资本 
    ///</summary>
    public decimal? RegCapital { get; set; }
    /// <summary>
    /// 企业注册日期 
    ///</summary>
    public DateTime RegisterDate { get; set; }
    /// <summary>
    /// 企业备注 
    ///</summary>
    public string Remark { get; set; }
    /// <summary>
    /// 创建时间 
    ///</summary>
    public DateTime? CreateTime { get; set; }

    /// <summary>
    /// 默认项目ID
    /// </summary>
    public int ProjectId { get; set; }
    /// <summary>
    /// 单位类型
    /// </summary>
    public ConstructorType Type { get; set; }
    /// <summary>
    /// 企业类型名称
    /// </summary>
    public string? TypeName { get; set; }

    /// <summary>
    /// 审核状态 0：待审核 1：审核通过 -1：审核失败
    /// </summary>
    public 审核状态 CheckStatus { get; set; }

    /// <summary>
    /// 审核状态
    /// </summary>
    public string CheckStatusName { get; set; } = string.Empty;

    /// <summary>
    /// 审核失败原因
    /// </summary>
    public string FailureReason { get; set; } = string.Empty;
}

public static partial class CorpInfoExceptions
{
    /// <summary>
    /// 转换为详细信息（带企业附件）
    /// </summary>
    /// <param name="corpInfo"></param>
    /// <param name="applicationContext"></param>
    /// <returns></returns>
    public static CorpDetailDto AsDetailDto(this CorpInfo corpInfo, IApplicationContext applicationContext)
    {
        CorpDetailDto corpInfoDto = new();
        var areaService = applicationContext.GetService<AreaService>();
        corpInfoDto.AreaName = areaService.GetAreaFullName(corpInfo.ProvinceCode, corpInfo.CityCode, corpInfo.AreaCode);
        corpInfoDto.DataSourceName = corpInfo.DataSource?.GetDescription();
        corpInfoDto.EconomicTypeName = corpInfo.EconomicType.GetDescription();
        corpInfoDto.TypeName = corpInfo.Type.GetDescription();
        applicationContext.GetService<IMapper>().Map(corpInfo, corpInfoDto);
        corpInfoDto.CheckStatusName = corpInfo.CheckStatus.GetDescription();
        var corpAttachmentService = applicationContext.GetService<CorpAttachmentService>();
        var attachments = corpAttachmentService.GetSysAttachmentByObjectIdAndType(corpInfo.CorpId, 企业附件类型.营业执照, 企业附件租户.Corp);
        foreach (var item in attachments)
        {
            AttachmentsForm<int> attachmentsForm = new()
            {
                AttachmentId = item.Id,
                AttachmentName = item.FriendlyName,
            };
            corpInfoDto.Attachments.Add(attachmentsForm);
        }
        return corpInfoDto;
    }

    /// <summary>
    /// 转换为基本信息（不显示隐私信息）
    /// </summary>
    /// <param name="corpInfo"></param>
    /// <returns></returns>
    public static CorpBaseInfoDto AsBaseInfoDto(this CorpInfo corpInfo, IApplicationContext applicationContext)
    {
        CorpBaseInfoDto corpInfoDto = new();
        var areaService = applicationContext.GetService<AreaService>();
        corpInfoDto.CorpId = corpInfo.CorpId;
        corpInfoDto.Code = corpInfo.Code;
        corpInfoDto.Name = corpInfo.Name;
        corpInfoDto.ProvinceCode = corpInfo.ProvinceCode;
        corpInfoDto.CityCode = corpInfo.CityCode;
        corpInfoDto.AreaCode = corpInfo.AreaCode;
        corpInfoDto.AreaName = areaService.GetAreaFullName(corpInfo.ProvinceCode, corpInfo.CityCode, corpInfo.AreaCode);
        corpInfoDto.RegAddr = corpInfo.RegAddr;
        corpInfoDto.LinkMan= corpInfo.LinkMan;
        corpInfoDto.LinkTel= corpInfo.LinkTel;
        return corpInfoDto;
    }

    /// <summary>
    /// 转换为相对完整信息
    /// </summary>
    /// <param name="corpInfo"></param>
    /// <param name="applicationContext"></param>
    /// <returns></returns>
    public static CorpInfoDto AsDto(this CorpInfo corpInfo, IApplicationContext applicationContext)
    {
        CorpInfoDto corpInfoDto = new();
        var areaService = applicationContext.GetService<AreaService>();
        corpInfoDto.AreaName = areaService.GetAreaFullName(corpInfo.ProvinceCode, corpInfo.CityCode, corpInfo.AreaCode);
        corpInfoDto.EconomicTypeName = corpInfo.EconomicType.GetDescription();
        corpInfoDto.TypeName = corpInfo.Type.GetDescription();
        corpInfoDto.CheckStatusName = corpInfo.CheckStatus.GetDescription();
        applicationContext.GetService<IMapper>().Map(corpInfo, corpInfoDto);

        return corpInfoDto;
    }


    public static LabelValue<int> AsPullDownDto(this CorpInfo entity)
    {
        LabelValue<int> data = new LabelValue<int>();
        data.Value = entity.CorpId;
        data.Label = entity.Name;
        data.Extend1 = entity.Code;
        return data;
    }
}
